<link href="<?php echo URL::base() ?>media/vendor/css/ui-lightness/jquery-ui-1.10.4.custom.css" rel="stylesheet">
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-1.7.1.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.core.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.widget.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.position.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.autocomplete.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-1.7.1.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-1.10.4.custom.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-timepicker-addon.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-timepicker-zh-CN.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.datepicker-zh-TW.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/Validform_v5.3.2_ncr_min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/artdialog/jquery.artDialog.js"></script>
<script>

/**
 * 显示供应商列表
 */
function show_supplier_lists(supplier_data)
{
    if(supplier_data.supplier_view)
    {
        $("#supplier_id").empty();
        $("#supplier_id").append(supplier_data.supplier_view);
    }
}

/**
 * 显示供应商详细信息
 */
function show_supplier_info(supplier_info) {
    if (supplier_info.supplier_contanct) {
        /**
         * 修改替换联系人选择框
         */
        $("#supplier_contact").empty();
        $("#supplier_contact").append(supplier_info.supplier_contanct);
    }

    if (supplier_info.supplier_payway) {
        /**
         * 修改替换付款方式选择框
         */
        $("#select_payway").empty();
        $("#select_payway").append(supplier_info.supplier_payway);
    }


    if (supplier_info.supplier_paybank) {
        /**
         * 修改替换付款帐号选择框
         */
        $("#select_paybankinfo").empty();
        $("#select_paybankinfo").append(supplier_info.supplier_paybank);
    }
}

function show_supplier_sku_info(supplier_sku_info)
{
    var id = 1;
    if(supplier_sku_info)
    {
        if(supplier_sku_info.sku)
        {
            $("#"+id).children().eq(0).find("input").val(supplier_sku_info.sku);
        }
        if(supplier_sku_info.sku_name)
        {
            $("#"+id).children().eq(1).text(supplier_sku_info.sku_name);
        }
        if(supplier_sku_info.nuit)
        {
            $("#"+id).children().eq(2).text(supplier_sku_info.nuit);
        }
        if(supplier_sku_info.place)
        {
            $("#"+id).children().eq(3).text(supplier_sku_info.place);
        }
        if(supplier_sku_info.price)
        {
            $("#"+id).children().eq(5).find("input").val(supplier_sku_info.price);
        }
        if(supplier_sku_info.delivery)
        {
            $("#"+id).children().eq(9).find("input").val(supplier_sku_info.delivery);
        }
        if(supplier_sku_info.sku_price)
        {
            $("#"+id).children().eq(10).text(supplier_sku_info.sku_price);
        }
        if(supplier_sku_info.status)
        {
            $("#"+id).children().eq(11).text(supplier_sku_info.status);
        }
    }
}

/**
 * 获取供应商sku信息，以及sku信息
 */
function get_supplier_sku_info(supplier_id,sku)
{

    if(supplier_id
        && sku)
    {
        $.ajax(
            {
                type: 'post',
                url: '<?php echo url::base(true)?>purchase/getskuinfo',
                dataType: 'json',
                data: {"supplier_id": supplier_id,"sku":sku},
                async: false,
                success: function (re_data) {
                    if(re_data){
                        show_supplier_sku_info(re_data);
                    }
                },
                error: function () {
                }
            }
        );
    }
}
/**
 * 根据供应商名称查询供应商的信息
 * @param supplier_id
 */
function get_supplier_info(supplier_id) {
    if(supplier_id){
        $.ajax(
            {
                type: 'post',
                url: '<?php echo url::base(true)?>purchase/supplierpayway',
                dataType: 'json',
                data: {"supplier_id": supplier_id},
                async: false,
                success: function (re_data) {
                    if(re_data)
                    {
                        show_supplier_info(re_data);
                    }
                },
                error: function () {
                }
            }
        );
    }
}

/**
 * 通过sku查询供应商和sku信息
 */
function select_sku_supplier(sku) {
    $.ajax(
        {
            type: 'post',
            url: '<?php echo url::base(true)?>purchase/get_sku_info',
            dataType: 'json',
            data: {"sku": sku},
            async: false,
            success: function (re_data) {
                if(re_data){
                    /**
                     * 显示供应商列表
                     */
                    show_supplier_lists(re_data);
                    if(re_data.supplier_dafault_id)
                    {
                        get_supplier_info(re_data.supplier_dafault_id);
                        get_supplier_sku_info(re_data.supplier_dafault_id,sku);
                    }
                }

            },
            error: function () {
            }
        }
    );
}

/**
 * 选择供应商事件处理
 */
function select_supplier(obj)
{
    var sku = $("#sku_input").val();
    var supplier_id = $(obj).val();
    get_supplier_info(supplier_id);
    get_supplier_sku_info(supplier_id,sku);
}
/**
 * 获取创建采购单的供应商信息
 */
function get_purchase_info_supplier()
{
    var supplier_id = $('#supplier_id').val();
    if(supplier_id == 0)
    {
//      alert('请选择供应商');return false;
    }

    var supplier_contact = $("#supplier_contact").val();
    if(supplier_contact == 0)
    {
//        alert('请选择供应商联系人');return false;
    }
    var select_payway = $("#select_payway").val();
    if(select_payway == 0)
    {
//        alert('请选择供应商付款方式');return false;
    }

    var select_paybankinfo = $("#select_paybankinfo").val();
    if(select_paybankinfo == 0)
    {
//        alert('请选择供应商付款帐号');return false;
    }

    var remark = $("#remark").val();
    var transportation_expense = $("#transportation_expense").val();
    /**
     *  供应商信息
     *  供应商id
     *  付款方式id
     *  付款帐号id
     *  运费
     *  备注
     */
    var supplier_info = {"supplier_id":supplier_id,
        "contact_id":supplier_contact,
        "pay_way":select_payway,
        "payaccountinfo_id":select_paybankinfo,
        "transportation_expense":transportation_expense,
        "remark":remark,
        "createtype":2
    };

    return supplier_info;
}

/**
 *  获取某一行的数据
 *
 */
function get_purchase_sku_info_row(id)
{
    if($("#"+id).children('td').find('input').attr('readonly'))
    {
        var sku_info= {
            "sku":$("#"+id).children('td').eq(0).find("input").val(),
            "sku_name":$("#"+id).children('td').eq(1).text(),
            "storage_position":$("#"+id).children('td').eq(3).text(),
            "sku_standard":"",
            "quantity":$("#"+id).children('td').eq(6).find('input').val(),
            "single_price":$("#"+id).children('td').eq(5).find('input').val(),
            "tax_rate":$("#"+id).children('td').eq(7).find('input').val(),
            "remark":"",
            "arrive_date":$("#"+id).children('td').eq(9).find('input').val(),
            "tax_way":$("#"+id).children('td').eq(4).find("select").val(),
            "sku_unit":$("#"+id).children('td').eq(1).text(),
            "prime_cost":$("#"+id).children('td').eq(10).text(),
            "supplier":$('#supplier_id').val()
        }

        return sku_info;
    }
    else
    {
        return false;
    }
}


/**
 * 获取采购单的SKU信息
 */
function get_purchase_sku_info()
{
    var sku_info_arr = [];
    $("#row").children('tr').each(function(){
        var sku_info = get_purchase_sku_info_row($(this).attr('id'));
        if(sku_info)
        {
            sku_info_arr.push(sku_info);
        }
    });

    if(sku_info_arr)
    {
        return sku_info_arr;
    }
    else
    {
        alert('请选择SKU');
        return false;
    }
}

function init_view()
{
    var sku=$("#sku_input").val();
    $("#input_sku").attr("readonly", "readonly");
    select_sku_supplier(sku);
    return false;
}

$(document).ready(function () {

    init_view();
    /**
     * 设置时间控件
     */
    $.datepicker.setDefaults({ dateFormat: 'yy-mm-dd' });

    $(document).on('focus', ".datepicker", function (e) {
        $(this).datepicker({
            minDate: new Date()
        });
    });



    $("#tijiao").click(function(){
        var supplier_info = get_purchase_info_supplier();
        var sku_info = get_purchase_sku_info();

        if(supplier_info && sku_info)
        {
            $.ajax(
                {
                    type: 'post',
                    url: '<?php echo url::base(true)?>purchase/createpurchasenew',
                    dataType: 'json',
                    data: {"purchaseorders": supplier_info,"purchaseorder_details":sku_info},
                    async: false,
                    success: function (re_data) {
                        if(re_data.code == 2)
                        {
                            var tips = '创建成功，采购单号'+re_data.id;
                            alert(tips);
                            window.location.reload();
                        }
                    },
                    error: function () {
                    }
                }
            );
        }
    });
});
</script>

<table class="table table-bordered" style="table-layout:fixed;">
    <tbody>
    <tr style=" background-color:#4EA392;color:#fff;">
        <th>
            SKU
        </th>
        <th>
            中文名称
        </th>
        <th>
            下单提示
        </th>
        <th>
            实际可用库存
        </th>
        <th>
            在途库存
        </th>
        <th>
            日均销量
        </th>
        <th>
            可支持天数
        </th>
        <th>
            误差天数
        </th>
        <th>
            最佳下单日期
        </th>
        <th>
            默认目标时间段
        </th>
        <th>
            建议采购量
        </th>
    </tr>

    <?php if(isset($skulists) and $skulists){ ?>
        <tr>
            <th>
               <?php echo isset($skulists['sku'])?$skulists['sku']:''?>
            </th>
            <th>
                <!--中文名称-->
                <?php echo isset($skulists['cnname'])?$skulists['cnname']:''?>
            </th>
            <th>
                <!--下单提示-->
                <?php echo isset($skulists['tips'])?$skulists['tips']:''?>
            </th>
            <th>
                <!--实际可用库存-->
                <?php echo isset($skulists['inventory']['actual_available_num'])?$skulists['inventory']['actual_available_num']:''?>
            </th>
            <th>
                <!--在途库存-->
                <?php echo isset($skulists['inventory']['pp_num'])?$skulists['inventory']['pp_num']:''?>
            </th>
            <th>
                <!--日均销量-->
                <?php echo isset($skulists['daysales'])?$skulists['daysales']:''?>
            </th>
            <th>
                <!--可支持天数-->
                <?php echo isset($skulists['supp_days'])?$skulists['supp_days']:''?>
            </th>
            <th>
                <!--误差天数-->
                <?php echo isset($skulists['misdays'])?$skulists['misdays']:''?>
            </th>
            <th>
                <!--最佳下单日期-->
                <?php echo isset($skulists['best'])?$skulists['best']:''?>
            </th>
            <th>
                <!--默认目标时间段-->
                <?php echo isset($skulists['target_time'])?$skulists['target_time']:''?>/天
            </th>
            <th>
                <!--建议采购量-->
                <?php echo isset($skulists['suggest'])?$skulists['suggest']:''?>
            </th>
        </tr>
    <?php } ?>
</table>
</div>

<div class="form-horizontal">
    <input type="hidden" id="sku_input" value="<?php echo $_GET['sku']?>">
    <div class="control-group">
        <label class="control-label" for="supplier_name">供应商:</label>

        <div class="controls" id="group">
            <select class="input-xlarge" name="supplier_name" id="supplier_id" style="width: 284px"
                    onchange="select_supplier(this)">
                <option value="0">选择供应商</option>
            </select>
        </div>
    </div>
    <div class="control-group" id="select_supplier_contact_div">
        <label class="control-label" for="supplier_contact">联系人:</label>

        <div class="controls">
            <select class="input-xlarge" name="supplier_contact" id="supplier_contact" style="width: 284px">
                <option value="0">选择联系人</option>
            </select>
        </div>
    </div>
    <div class="control-group" id="supplier_payway_div">
        <label class="control-label" for="input01">付款方式:</label>

        <div class="controls">
            <select class="input-xlarge" name="paytype" id="select_payway" style="width: 284px">
                <option value="0">选择付款方式</option>
            </select>
        </div>
    </div>
    <div class="control-group" id="select_paybankinfo_div">
        <label class="control-label" for="select_paybankinfo">付款帐号:</label>

        <div class="controls">
            <select class="input-xlarge" name="select_paybankinfo" id="select_paybankinfo" style="width: 284px">
                <option  value="0">选择付款帐号</option>
            </select>
        </div>
    </div>

    <div class="control-group">
        <label class="control-label" for="input01">运费:</label>

        <div class="controls">
            <input type="text" class="input-xlarge" onkeyup="clearNoNum(this)" name="transportation_expense"
                   id="transportation_expense" value="0"/>
        </div>
    </div>
    <div class="control-group">

        <!-- Textarea -->
        <label class="control-label">备注:</label>

        <div class="controls">
            <textarea type="" class="" style="margin: 0px; width: 272px; height: 90px;resize:none;" name="remark"
                      id="remark"> </textarea>
        </div>
    </div>
    <div class="control-group" style="display:none">
        <label class="control-label" for="input01"></label>

        <div class="controls">
            <input type="text" class="input-xlarge" name="supplier" id="supplier" value=""/>
        </div>
    </div>
</div>
<table class="table table1 table-hover table-striped table-condensed">
    <thead>
    <tr style="  background-color:#4EA392; color:#fff;">

        <th>
            SKU
        </th>
        <th>
            中文名称
        </th>
        <th>
            单位
        </th>
        <th>
            储位
        </th>
        <th>
            计税方式
        </th>
        <th>
            单价
        </th>
        <th>
            数量
        </th>
        <th>
            税率
        </th>
        <th>
            金额
        </th>
        <th>
            交货日期
        </th>
        <th>
            系统成本价
        </th>
        <th>
            状态
        </th>


    </tr>
    </thead>
    <tbody id="row">
    <tr id="1">

        <td>
            <input style="width:70px;" class="input-mini sku_input" type="text" id="input_sku"
                   value="<?php echo $skulists[0]['sku'] ?>" autocomplete="off"
                />
        </td>
        <td>

        </td>
        <td>

        </td>
        <td>

        </td>

        <td>
            <select class="input-xlarge tax_way" name="tax_way" id="tax_way" style="width: 75px" autocomplete="off">
                <?php if(isset($tax_ways) and $tax_ways){
                    foreach($tax_ways as $tax_way){
                        ?>
                        <option value="<?php echo $tax_way['id'] ?>">
                            <?php echo $tax_way['tax_way'] ?>
                        </option>
                    <?php
                    }
                } ?>
            </select>
        </td>
        <td>
            <input class="input-mini" type="text" value='' style="width:40px" onBlur='atotle_s_price(this)'
                   autocomplete="off"/>
        </td>
        <td>
            <input class="input-mini" type="text" value='' style="width:40px" onkeyup="key_up(this)"
                   autocomplete="off"
                   onafterpaste="key_paste()" onBlur="atotle_price(this)"/>
        </td>
        <td>
            <input class="input-mini tax_rate" onkeyup="clearNoNum(this)" type="text" value='0' autocomplete="off"/>%
        </td>
        <td>
            <input class="input-mini" type="text" value='' disabled="disabled" autocomplete="off"/>
        </td>
        <td>
            <input class="input-mini datepicker" type="text"
                   value="<?php echo date('Y-m-d',strtotime($supplier_for_sku[0]['delivery'].'days')) ?>"
                   style="width:80px" autocomplete="off"/>
        </td>
        <td>

        </td>
        <td>

        </td>

    </tr>

    </tbody>

</table>
<button id="tijiao" class="btn btn-info" style="margin-left:10px;">一键创建</button>
